home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWFoundU / Include / FWCouPtr.h next >
Encoding:
Text File  |  1995-11-08  |  5.0 KB  |  171 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWCouPtr.h
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    (c) 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef FWCOUPTR_H
  11. #define FWCOUPTR_H
  12.  
  13. #ifndef FWAUTODE_H
  14. #include "FWAutoDe.h"
  15. #endif
  16.  
  17. #if FW_LIB_EXPORT_PRAGMAS
  18. #pragma lib_export on
  19. #endif
  20.  
  21. //========================================================================================
  22. // Forward class declaration
  23. //========================================================================================
  24.  
  25. class FW_CLASS_ATTR FW_CCountedPtrRep;
  26.  
  27. //========================================================================================
  28. // CLASS FW_CCountedPtr
  29. //========================================================================================
  30.  
  31. class FW_CLASS_ATTR FW_CCountedPtr FW_AUTO_DESTRUCT_OBJECT
  32. {
  33. public:
  34.     FW_CCountedPtr();
  35.         // Creates a "NULL" pointer.
  36.  
  37.     virtual ~ FW_CCountedPtr();
  38.         // Destroy the pointer.  
  39.         // Decrements the count in the rep, and deletes the rep if count is zero.
  40.         
  41.     FW_CCountedPtr(const FW_CCountedPtr& other);
  42.         // FW_SPlatformPoint this pointer to the same rep as the other pointer points to.
  43.  
  44.     FW_CCountedPtr(FW_CCountedPtrRep* rep);
  45.         // Creates a pointer pointing at rep.
  46.     
  47.     FW_CCountedPtr& operator=(const FW_CCountedPtr& other);
  48.         // Assignment, point this ponter to the same rep as the other pointer points to.
  49.         
  50.     FW_CCountedPtr& operator=(FW_CCountedPtrRep* rep);
  51.         // Assignment, point this ponter to rep.
  52.         
  53.     FW_CCountedPtrRep* operator->() const;    // Must be overlodded by subclass
  54.         // Provide access to members of rep.    
  55.         
  56.     FW_CCountedPtrRep& operator*() const;
  57.         // Provide access to rep.
  58.         // This operator should be used only if operator->() is not sufficient.
  59.     
  60.     FW_Boolean operator==(const FW_CCountedPtr& other) const;
  61.         // Points to the same representation object?
  62.  
  63.     FW_Boolean operator!=(const FW_CCountedPtr& other) const;
  64.         // Points to a different representation object?
  65.  
  66.     operator const void*() const;
  67.         // Use to test if this is a "NULL" pointer.
  68.  
  69. protected:
  70.     void    SetRep(FW_CCountedPtrRep* rep);
  71.     
  72. private:
  73.  
  74.     void UpCount();
  75.         // Implementation method, increment the reference count in rep.
  76.         
  77.     void DownCount();
  78.         // Implementation method, decrement the reference count in rep.
  79.         // Deletes rep if reference count is zero.
  80.  
  81. protected:    
  82.     FW_CCountedPtrRep *fRep;
  83. };
  84.  
  85. //----------------------------------------------------------------------------------------
  86. // FW_CCountedPtr::operator->
  87. //----------------------------------------------------------------------------------------
  88.  
  89. inline FW_CCountedPtrRep* FW_CCountedPtr::operator->() const
  90. {
  91.     return fRep;
  92. }
  93.  
  94. //----------------------------------------------------------------------------------------
  95. // FW_CCountedPtr::operator*
  96. //----------------------------------------------------------------------------------------
  97.  
  98. inline FW_CCountedPtrRep& FW_CCountedPtr::operator*() const
  99. {
  100.     return *fRep;
  101. }
  102.  
  103. //----------------------------------------------------------------------------------------
  104. // FW_CCountedPtr::operator==
  105. //----------------------------------------------------------------------------------------
  106.  
  107. inline FW_Boolean FW_CCountedPtr::operator==(const FW_CCountedPtr& other) const
  108. {
  109.     return (fRep == other.fRep);
  110. }
  111.  
  112. //----------------------------------------------------------------------------------------
  113. // FW_CCountedPtr::operator!=
  114. //----------------------------------------------------------------------------------------
  115.  
  116. inline FW_Boolean FW_CCountedPtr::operator!=(const FW_CCountedPtr& other) const
  117. {
  118.     return !operator==(other);
  119. }
  120.  
  121. //----------------------------------------------------------------------------------------
  122. // FW_CCountedPtr::operator const void*
  123. //----------------------------------------------------------------------------------------
  124.  
  125. inline FW_CCountedPtr::operator const void*() const
  126. {
  127.     return (const void*) fRep;
  128. }
  129.  
  130. //========================================================================================
  131. // CLASS FW_CCountedPtrRep
  132. //========================================================================================
  133.  
  134. class FW_CLASS_ATTR FW_CCountedPtrRep
  135. {
  136. public:
  137.         
  138.     FW_CCountedPtrRep();
  139.     virtual ~ FW_CCountedPtrRep();
  140.  
  141.     long    IncrementReferenceCount();
  142.     long    DecrementReferenceCount();
  143.     
  144. private:
  145.     unsigned long     fRefCount;
  146. };
  147.  
  148. //----------------------------------------------------------------------------------------
  149. // FW_CCountedPtrRep::DecrementReferenceCount
  150. //----------------------------------------------------------------------------------------
  151.  
  152. inline long FW_CCountedPtrRep::DecrementReferenceCount()
  153. {
  154.     return --fRefCount;
  155. }
  156.  
  157. //----------------------------------------------------------------------------------------
  158. // FW_CCountedPtrRep::IncrementReferenceCount
  159. //----------------------------------------------------------------------------------------
  160.  
  161. inline long FW_CCountedPtrRep::IncrementReferenceCount()
  162. {
  163.     return ++fRefCount;
  164. }
  165.  
  166. #if FW_LIB_EXPORT_PRAGMAS
  167. #pragma lib_export off
  168. #endif
  169.  
  170. #endif
  171.